package com.shiliuzi.www.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shiliuzi.www.common.Code;
import com.shiliuzi.www.pojo.bo.UserInfoBo;
import com.shiliuzi.www.pojo.dto.user.UserSetRoleDto;
import com.shiliuzi.www.pojo.dto.user.UserUpMemberDto;
import com.shiliuzi.www.pojo.vo.user.UserSetRoleVo;
import com.shiliuzi.www.pojo.vo.user.UserUpLowMemberVo;
import com.shiliuzi.www.service.UserService;
import com.shiliuzi.www.common.Result;
import com.shiliuzi.www.mapper.MemberMapper;
import com.shiliuzi.www.pojo.dto.user.UserSelectDto;
import com.shiliuzi.www.pojo.dto.user.UserSelectResultDto;
import com.shiliuzi.www.pojo.po.User;
import com.shiliuzi.www.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.shiliuzi.www.common.Code.ERROR;
import static com.shiliuzi.www.common.Code.SUCCESS;
import javax.annotation.Resource;
import java.util.List;
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private MemberMapper memberMapper;

    @Resource
    private CommonUtil commonUtil;

    @Override
    public Result managerSelect(UserSelectDto userSelectDto) {
        User user = userSelectDto.getUser();
        int page = userSelectDto.getPage();
        int pageSize = userSelectDto.getPageSize();
        List<User> users = memberMapper.selUser(user);
        if(users.size() == 0){
            log.info("查无此人");
            return Result.error(ERROR,"查无此人");
        }
        PageHelper.startPage(page,pageSize);
        Page<User>p = (Page<User>)users;
        long total = p.getTotal();
        int totalPage = calculatePage(total,pageSize);
        UserSelectResultDto userSelectResultDto = new UserSelectResultDto(users,page,pageSize,total,totalPage);
        return Result.success(SUCCESS,userSelectResultDto);
    }
    private Integer calculatePage(long total,Integer pageSize){
        int allSize;
        if(total % pageSize != 0){
            allSize = (int)total/pageSize + 1;
        }
        else allSize =(int)total/pageSize;
        return allSize;
    }

    @Override
    //TODO 防止ABA问题
    public Result setMemberRole(UserSetRoleDto userSetRoleDto) {

        //参数再校验
        if (userSetRoleDto.getConvertRole() >= 3 && userSetRoleDto.getConvertRole() <= 0)
            return Result.error(Code.BUE_PARAM_ERR, "参数错误！请查看参数是否符合标准！");

        //设置身份
        if (memberMapper.setMemberRole(userSetRoleDto) == 0)
            return Result.error(Code.BUE_PARAM_ERR, "请查看用户学号是否正确！");

        return Result.success(Code.SUCCESS, new UserSetRoleVo("设置成功！该用户已经设为指定角色！"));
    }

    @Override
    public Result getUpLowMember(Integer type) {

        UserInfoBo userInfoBo = UserInfoBo.obtainUser();

        if (userInfoBo.getIdentity().equals("队长") && type.equals(0))
            return Result.error(Code.BUE_PARAM_ERR,"您是队长，没有上级！");

        if(userInfoBo.getIdentity().equals("队员") && type.equals(1))
            return Result.error(Code.BUE_PARAM_ERR,"您是队员，没有下级！");

        List<User> users = memberMapper.selectUpMember(userInfoBo.getId(), type);

        return Result.success(Code.SUCCESS,commonUtil.copyPropertiesList(commonUtil.copyPropertiesList(users,UserInfoBo.class), UserUpLowMemberVo.class));
    }

    @Override
    @Transactional
    //TODO 加锁
    public Result setUpMember(UserUpMemberDto userUpMemberDto) {

        if(userUpMemberDto.getUpNumber().equals(userUpMemberDto.getMinionNumber()))
            return Result.error(Code.BUE_PARAM_ERR,"传入的两个学号一致，无法进行设置！");

        //1、获取需设置上下级关系成员的身份
        Integer upIdentity = memberMapper.getMemberIdentity(userUpMemberDto.getUpNumber());
        Integer minionIdentity = memberMapper.getMemberIdentity(userUpMemberDto.getMinionNumber());

        if ((upIdentity.equals(minionIdentity) || upIdentity > minionIdentity) && userUpMemberDto.getChoose().equals(1))
            return Result.error(Code.BUE_PARAM_ERR,"身份出现差异！请重新设置上下级关系！");

        if (userUpMemberDto.getChoose().equals(1) && !memberMapper.judgeMemberUpLow(userUpMemberDto.getUpNumber(),userUpMemberDto.getMinionNumber()).equals(0) )
            return Result.error(Code.BUE_PARAM_ERR,"设置的下级为设置的上级的上级！请注意身份！");

        userUpMemberDto.setUpNumber(String.valueOf(memberMapper.getUpMemberId(userUpMemberDto.getUpNumber())));
        if (memberMapper.setUpMember(userUpMemberDto).equals(0))
            return Result.error(Code.SYS_ERR, "数据库宕机！请重新设置！");

        return Result.success(Code.SUCCESS,"成功设置！请注意查看！");
    }
}
